# -*- coding: utf-8 -*-#

# -------------------------------------------------------------------------------
# Name:         avenger
# Description:  复仇者；在这里处理不同站点的数据
# Author:       zhuchunxu
# Date:         2020/11/3
# -------------------------------------------------------------------------------
from spider_source.utils.job51_utils import Job51Utils
from spider_source.utils.mongodb_utils import MongodbUtils
from spider_source.utils.pzhrc_utils import Pzhrc1Utils
from spider_source.utils.redis_utils import RedisUtils
from spider_source.utils.scrc_utils import Scrc1Utils
from spider_source.utils.text_utils import TextUtils
from spider_source.utils.ybrc_utils import Ybrc1Utils
from spider_source.utils.yingjiesheng_utils import YingJieShengUtils
from spider_source.utils.zgrc_utils import Zgrc1Utils


class Avenger:
    @staticmethod
    def dispatcher(spider_name, response, **kwargs):
        if spider_name.startswith("job51"):
            job51_utils = Job51Utils()
            if kwargs.get("db_name") == "redis":
                page_detail_links = tuple(job51_utils.get_page_detail_links(response))
                # 把链接存入redis
                redis_utils = RedisUtils()
                redis_utils.insert(kwargs.get("redis_key"), *page_detail_links)
            elif kwargs.get("db_name") == "mongodb":
                job = job51_utils.get_page_detail_info(response)
                mongodb_utils = MongodbUtils()
                mongodb_utils.insert(job)
        elif spider_name.startswith("yingjieshengsichuan"):
            yingjiesheng = YingJieShengUtils()
            if kwargs.get("db_name") == "redis":
                page_detail_links = tuple(yingjiesheng.get_page_detail_links(response))
                # 把链接存入redis
                redis_utils = RedisUtils()
                redis_utils.insert(kwargs.get("redis_key"), *page_detail_links)
            elif kwargs.get("db_name") == "text":
                job = yingjiesheng.get_page_detail_info(response)
                # 这里预防以后可能存txt;目前实现功能是存csv，目的是为了后续使用spark直接使用df分析
                if kwargs.get("file_type") == "json":
                    TextUtils.insert_json(job, kwargs.get("file_name"))
        elif spider_name.startswith("scrc"):
            scrc_utils = Scrc1Utils()
            if kwargs.get("db_name") == "redis":
                page_detail_links = tuple(scrc_utils.get_page_detail_links(response))
                # 把链接存入redis
                redis_utils = RedisUtils()
                redis_utils.insert(kwargs.get("redis_key"), *page_detail_links)
            elif kwargs.get("db_name") == "mongodb":
                job = scrc_utils.get_page_detail_info(response)
                # mongodb_utils = MongodbUtils()
                # mongodb_utils.insert(job)
        elif spider_name.startswith("zgrc"):
            zgrc_utils = Zgrc1Utils()
            if kwargs.get("db_name") == "redis":
                page_detail_links = tuple(zgrc_utils.get_page_detail_links(response))
                # 把链接存入redis
                redis_utils = RedisUtils()
                redis_utils.insert(kwargs.get("redis_key"), *page_detail_links)
            elif kwargs.get("db_name") == "mongodb":
                job = zgrc_utils.get_page_detail_info(response)
                # mongodb_utils = MongodbUtils()
                # mongodb_utils.insert(job)
        elif spider_name.startswith("pzhrc"):
            pzhrc_utils = Pzhrc1Utils()
            if kwargs.get("db_name") == "redis":
                page_detail_links = tuple(pzhrc_utils.get_page_detail_links(response))
                # 把链接存入redis
                print(page_detail_links)
                redis_utils = RedisUtils()
                redis_utils.insert(kwargs.get("redis_key"), *page_detail_links)
            elif kwargs.get("db_name") == "mongodb":
                job = pzhrc_utils.get_page_detail_info(response)
                # mongodb_utils = MongodbUtils()
                # mongodb_utils.insert(job)
        elif spider_name.startswith("ybrc"):
            ybrc_utils = Ybrc1Utils()
            if kwargs.get("db_name") == "redis":
                page_detail_links = tuple(ybrc_utils.get_page_detail_links(response))
                # 把链接存入redis
                print(page_detail_links)
                redis_utils = RedisUtils()
                redis_utils.insert(kwargs.get("redis_key"), *page_detail_links)
            elif kwargs.get("db_name") == "mongodb":
                job = ybrc_utils.get_page_detail_info(response)
                # mongodb_utils = MongodbUtils()
                # mongodb_utils.insert(job)